package com.tourism.website.personal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tourism.website.personal.entity.User;
import com.tourism.website.personal.mapper.UserMapper;
import com.tourism.website.personal.service.IUserService;
import com.tourismwebsite.personal.dto.PersonalDTO;
import com.tourismwebsite.personal.query.PersonalQuery;
import com.tourismwebsite.vo.JsonVO;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kevin
 * @since 2024-06-11
 */
@Mapper(componentModel="spring")
interface MsPersonalMapper{
    PersonalDTO personalToPersonalDTO(User user);
}


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    UserMapper userMapper;
    @Resource
    IUserService iUserService;
    @Resource
    MsPersonalMapper msPersonalMapper;

    //更新个人信息
    @Override
    public boolean updatePersonal(PersonalQuery personalQuery) {
        int result = userMapper.update(personalQuery.getNickName(),
                personalQuery.getEmail(),personalQuery.getPhonenumber(),
                personalQuery.getSex(),personalQuery.getAvatar(),personalQuery.getPassword(),
                personalQuery.getUserName());
        return result > 0;
    }
    @Override
    public List<PersonalDTO> queryPersonal(String userName) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(User::getUserId ,User::getUserName,User::getAvatar,User::getEmail,User::getNickName,
                User::getPhonenumber,User::getSex,User::getPassword);
        wrapper.eq(User::getUserName,userName);
        List<User> Users = baseMapper.selectList(wrapper);
        List<PersonalDTO> personalDTOS = Users.stream()
                .map(User -> msPersonalMapper.personalToPersonalDTO(User))
                .collect(Collectors.toList());
        return personalDTOS;
    }
    @Override
    public List<PersonalDTO> queryPersonalById(Integer userId) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(User::getAvatar,User::getNickName);
        wrapper.eq(User::getUserId,userId);
        List<User> Users = baseMapper.selectList(wrapper);
        List<PersonalDTO> personalDTOS = Users.stream()
                .map(User -> msPersonalMapper.personalToPersonalDTO(User))
                .collect(Collectors.toList());
        return personalDTOS;
    }
}
